// vue.config.js
let filePath = "";
let Timestamp = "";

// 编译环境判断,判断是否H5环境
if (process.env.UNI_PLATFORM === "h5") {
  filePath = "static/js/"; // 打包文件存放文件夹路径
  Timestamp = "." + new Date().getTime();// 时间戳
}

const path = require("path");
function resolve(dir) {
  return path.join(__dirname, dir);
}
const TransformPages = require("uni-read-pages");
const { webpack } = new TransformPages();

const CompressionPlugin = require("compression-webpack-plugin")
module.exports = {
  configureWebpack: {
    plugins: [
      new webpack.DefinePlugin({
        ROUTES: webpack.DefinePlugin.runtimeValue(() => {
          const tfPages = new TransformPages({
            includes: ["path", "name", "aliasPath"]
          });
          return JSON.stringify(tfPages.routes);
        }, true)
      }),
      new CompressionPlugin(
        {
          filename: "[path][base].gz",
          algorithm: "gzip",
          test: /\.(js|css|less)$/,
          threshold: 10240,
          deleteOriginalAssets: false, // 不删除源文件
          minRatio: 0.8,
          exclude: /node_modules/,
        }
      )
    ],
    resolve: {
      alias: {
        "$utils": resolve("/utils"),
        "$api": resolve("/config/api"),
        "$pages": resolve("/pages"),
        "$configParams": resolve("/configParams"),
        "$common": resolve("/common"),
      }
    },
    output: {
      // 输出重构  打包编译后的 文件目录 文件名称 【模块名称.时间戳】 解决缓存问题
      // uni-app h5需要配置自定义模板 (关联 template.h5.html)
      filename: `${filePath}[name]${Timestamp}.js`,
      chunkFilename: `${filePath}[name]${Timestamp}.js`
    }
  },
  // chainWebpack: (config) => {
  //   // 生产环境，开启js\css压缩
  //   if (process.env.NODE_ENV === "production") {
  //     config.plugin("compressionPlugin").use(new CompressionPlugin({
  //       test: /\.(js|css|less)$/, // 匹配文件名
  //       threshold: 10240, // 对超过10k的数据压缩
  //       deleteOriginalAssets: false, // 不删除源文件
  // exclude: /node_modules/,
  // minRatio: 0.5,
  //     }))

  //   }
  // }
};
